- 애니메이션의 목적
- 시간의 변화를 표현
- 이산 변수의 값의 변화를 표현
ggplot객체를 이용하여 애니메이션 객체 제작- 최근에 CRAN에 등장했고, 발전 가능성이 높은 패키지
- 공식 페이지 https://gganimate.com/
anim_save()로gif파일 제작 가능 (ggsave()와 유사)
gganimateggplot 객체를 이용하여 애니메이션 객체 제작
anim_save()로 gif 파일 제작 가능 (ggsave()와 유사)transition_time() - 시간의 변화에 따른 데이터의 변화를 표현transition_states() - 이산 변수 값의 변화에 따른 데이터의 변화를 표현transition_reveal()library(tidyverse) library(plotly) library(DT) library(gganimate) library(gifski)
transition_time()transition_states()transition_reveal()transition_time()gapminder 데이터셋library(gapminder) datatable(gapminder)
fig1 <- gapminder %>% filter(year==2007) %>% ggplot(aes(x = gdpPercap, y=lifeExp, size = pop, colour = country)) + geom_point(show.legend = FALSE, alpha = 0.7) + scale_color_viridis_d() + scale_size(range = c(2, 12)) + scale_x_log10() + labs(x = "GDP per capita", y = "Life expectancy") fig1
facet_wrap()으로 애니메이션 구상fig2 <- gapminder %>% ggplot(aes(x = gdpPercap, y=lifeExp, size = pop, colour = country)) + geom_point(show.legend = FALSE, alpha = 0.7) + scale_color_viridis_d() + scale_size(range = c(2, 12)) + scale_x_log10() + labs(x = "GDP per capita", y = "Life expectancy") + facet_wrap(~ year)
fig1 <- gapminder %>% filter(year==2007) %>% ggplot(aes(x = gdpPercap, y=lifeExp, size = pop, colour = country)) + geom_point(show.legend = FALSE, alpha = 0.7) + scale_color_viridis_d() + scale_size(range = c(2, 12)) + scale_x_log10() + labs(x = "GDP per capita", y = "Life expectancy")
fig2
fig2에서 facet_wrap(~ year)을 빼서 insensible한 static ggplot 객체를 생성fig_static <- gapminder %>% ggplot(aes(x = gdpPercap, y=lifeExp, size = pop, colour = country)) + geom_point(show.legend = FALSE, alpha = 0.7) + scale_color_viridis_d() + scale_size(range = c(2, 12)) + scale_x_log10() + labs(x = "GDP per capita", y = "Life expectancy")
fig2 <- gapminder %>% ggplot(aes(x = gdpPercap, y=lifeExp, size = pop, colour = country)) + geom_point(show.legend = FALSE, alpha = 0.7) + scale_color_viridis_d() + scale_size(range = c(2, 12)) + scale_x_log10() + labs(x = "GDP per capita", y = "Life expectancy") + facet_wrap(~ year)
fig_static
fig_static은 그야말로 insensible 하지만…fig_dynamic <- fig_static +
transition_time(year) +
labs(title = "Year: {frame_time}")
class(fig_dynamic)
## [1] "gganim" "gg" "ggplot"
facet_wrap() 대신에 transition_time()을 사용한다고 이해하면 쉬움anim_save(filename = "anim_gapminder.gif",
animation = fig_dynamic,
renderer = gifski_renderer())
include_graphics("anim_gapminder.gif")
library(tidytext)
# Filter G7 countries
G7 <- gapminder %>%
filter(
country %in% c("Canada", "France", "Germany", "Italy",
"Japan", "United Kingdom", "United States"))
facet_wrap()으로 애니메이션 구상fig_facet <- G7 %>% ggplot(aes(x = reorder(country, gdpPercap), y = gdpPercap, fill = country)) + geom_col() + coord_flip() + labs(x = NULL, y = "GDP per capita") + theme(legend.position = "none") + facet_wrap(~ year)
fig_facet
fig_static 만들기fig_static <- G7 %>% ggplot(aes(x = reorder(country, gdpPercap), y = gdpPercap, fill = country)) + geom_col() + coord_flip() + labs(x = NULL, y = "GDP per capita") + theme(legend.position = "none")
fig_dynamic 만들고 저장fig_dynamic <-
fig_static + transition_time(year) + labs(title = "Year: {frame_time}")
anim_save(filename = "anim_gdp.gif",
animation = fig_dynamic,
renderer = gifski_renderer())
include_graphics("anim_gdp.gif")
transition_states()transition_states()facet_wrap()의 각 facet들이 번갈아가면서 보여짐transition_time()과 비슷하게 아래의 3단계 접근을 사용
facet_wrap()으로 애니메이션 구상fig_static 만들기fig_dynamic 만들고 저장facet_wrap()으로 애니메이션 구상fig1 <- gapminder %>% filter(year==2007) %>% ggplot(aes(x = gdpPercap, y=lifeExp, size = pop, colour = continent)) + geom_point(show.legend = FALSE) + scale_size(range = c(2, 12)) + scale_x_log10() + labs(x = "GDP per capita", y = "Life expectancy") + facet_wrap(~ continent)
fig1
gapminder$continent <- factor(gapminder$continent,
levels = c("Africa", "Asia", "Americas", "Europe", "Oceania"))
fig1 <- gapminder %>% filter(year==2007) %>% ggplot(aes(x = gdpPercap, y=lifeExp, size = pop, colour = continent)) + geom_point(show.legend = FALSE) + scale_size(range = c(2, 12)) + scale_x_log10() + labs(x = "GDP per capita", y = "Life expectancy") + facet_wrap(~ continent)
fig1
fig_static 만들기fig_static <- gapminder %>% filter(year==2007) %>% ggplot(aes(x = gdpPercap, y=lifeExp, size = pop, colour = continent)) + geom_point(show.legend = FALSE) + scale_size(range = c(2, 12)) + scale_x_log10() + labs(x = "GDP per capita", y = "Life expectancy")
fig_dynamic 만들고 저장fig_dynamic <-
fig_static + transition_states(continent) + labs(title = "Now Showing {closest_state}")
anim_save(filename = "anim_continent.gif",
animation = fig_dynamic,
renderer = gifski_renderer())
transition_time()fig_dynamic <-
fig_static + transition_time(year) + labs(title = "Year: {frame_time}")
include_graphics("anim_continent.gif")
Takeaway:
facet_wrap()만 보면 animation을 생각해 보세요.노력에 비해서 훨씬 있어보임.
fig_static 만들기fig_static <- gapminder %>% filter(year %in% c(1957, 1982, 2007)) %>% ggplot(aes(x = gdpPercap, y=lifeExp, size = pop, colour = continent)) + geom_point(show.legend = FALSE) + scale_size(range = c(2, 12)) + scale_x_log10() + labs(x = "GDP per capita", y = "Life expectancy") + facet_wrap(~ year)
fig_static
fig_dynamic 만들고 저장fig_dynamic <-
fig_static + transition_states(continent) + labs(title = "Now Showing {closest_state}")
anim_save(filename = "anim_continent2.gif",
animation = fig_dynamic,
renderer = gifski_renderer(),
height = 350, width = 1200)
include_graphics("anim_continent2.gif")
transition_reveal()transition_reveal()fig_static 만들기fig_static <- gapminder %>% group_by(year, continent) %>% summarise(lifeExp = mean(lifeExp)) %>% ggplot(aes(x=year, y=lifeExp, group = continent, color = continent)) + geom_path()
fig_static
fig_dynamic 만들어서 저장fig_dynamic <- fig_static + transition_reveal(year) + geom_point()
anim_save(filename = "anim_year.gif", animation = fig_dynamic,
renderer = gifski_renderer(), height = 300, width = 500)
include_graphics("anim_year.gif")
fig_static 만들기gapminder$continent <- factor(gapminder$continent,
levels = c("Africa", "Americas", "Asia", "Europe", "Oceania"))
fig_static <- gapminder %>%
group_by(year, continent) %>%
summarise(gdpPercap = mean(gdpPercap)) %>%
ggplot(aes(x = factor(year), y = gdpPercap)) +
geom_col(aes(fill = continent), position = "dodge") +
theme(axis.title.x=element_blank())
fig_static
fig_dynamic 만들기fig_dynamic <- fig_static + transition_reveal(year)
anim_save(filename = "anim_year2.gif", animation = fig_dynamic,
renderer = gifski_renderer(), height = 300, width = 500)
include_graphics("anim_year2.gif")
The happiest people have a sense of commitment in everything they do. – Dr. Bob Rotella